using System.Linq;

namespace jSLApp.Web.Workflow
{

    

using System;
using System.Runtime.Serialization;
using jSLCore.Service.Data;

    public partial class SystemLog:BaseData
    {
        public override void BeforeSending()
        {
            TypeEnum = (LogType) Type;
        }

        [DataMember]
        public LogType TypeEnum { get; set; }
    }

    public partial class Role:BaseData{}

    public partial class Chat:BaseData
    {
        public override void BeforeSending()
        {
            CreatorNameTxt = User.GetName();
            
        }

        [DataMember]
        public string CreatorNameTxt { get; set; }

    }

    public partial class Subsription:BaseData
    {
        public override void BeforeSending()
        {
            NameTxt = string.Format("{0} - {1}",Commentable.User.GetName(),Commentable.Name);

            TypeEnum = Commentable.GetCommentableType();
        }

        [DataMember]
        public string NameTxt { get; set; }

        [DataMember]
        public CommentableType TypeEnum { get; set; }
    }

    public partial class Commentable:BaseData
    {
        public override void BeforeSending()
        {
            CreatorNameTxt = User.GetName();

            TypeEnum = this.GetCommentableType();
            
            var comment = Comments
                .OrderByDescending(c=>c.CreationDate)
                .FirstOrDefault();
            if (comment != null)
                LastMessageInfo = string.Format("{0} {1}", comment.CreationDate.ToString("dd.MM.yyyy hh:mm"),
                                                comment.User.GetName());
        }

        

        [DataMember]
        public string LastMessageInfo { get; set; }

        [DataMember]
        public string CreatorNameTxt { get; set; }

        [DataMember]
        public CommentableType TypeEnum { get; set; }
    }
    public partial class Comment : BaseData
    {
        public override void BeforeSending()
        {
            
            if (User.Image != null) CreatorImageBs = User.Image.ToArray();
            else
            {
                CreatorImageBs = MainServiceEx.GetNoPhoto();
            }

            CreatorNameText = User.GetName();

            if (Rate != null)
            {
                RateMusic = Rate.RateMusic;
                RateQuality = Rate.RateQuality;
                RateDance = Rate.RateDance;
            }
            
        }

        [DataMember]
        public byte[] CreatorImageBs { get; set; }

        [DataMember]
        public string CreatorNameText { get; set; }

        [DataMember]
        public double? RateMusic { get; set; }

        [DataMember]
        public double? RateDance { get; set; }

        [DataMember]
        public double? RateQuality { get; set; }
    }

    public partial class New : BaseData
    {
        public override void BeforeSending()
        {
            TypeEnum = (ArticleType) Type;
            CreatorNameText = Commentable.User.GetName();
            ImageBs = HeaderImage.ToArray();
            CreationDate = Commentable.CreationDate;
            Header = Commentable.Name;
        }

        [DataMember]
        public ArticleType TypeEnum { get; set; }

        [DataMember]
        public string Header { get; set; }

        [DataMember]
        public DateTime CreationDate { get; set; }

        [DataMember]
        public string CreatorNameText { get; set; }

        [DataMember]
        public byte[] ImageBs { get; set; }
    }

    public partial class User : BaseData
    {
        public override void BeforeSending()
        {
            if (Role != null)
                RoleText = Role.Name;

            Login = Login.Trim();
            PrepareName();
        }

        public void PrepareProject()
        {
            Login = Login.Trim();
            LoginTxt = Login.Trim();
            NameText = PrepareName();
        }

        public string PrepareName()
        {
            return this.GetName();
        }

        [DataMember]
        public string NameText { get; set; }

        [DataMember]
        public string RoleText { get; set; }

        [DataMember]
        public string LoginTxt { get; set; }
    }

    public partial class Message:BaseData
    {
        public override void BeforeSending()
        {
            FromText = Sender.GetName();
            ToText = Recepient.GetName();
        }

        [DataMember]
        public string FromText { get; set; }

        [DataMember]
        public string ToText { get; set; }
    }

    public partial class Topic : BaseData
    {
        public override void BeforeSending()
        {
            Header = Commentable.Name;
            CreatorNameText = Commentable.User.GetName();
            Creator = Commentable.User.Login;
            CreationDate = Commentable.CreationDate;
        }

        [DataMember]
        public string Header { get; set; }

        [DataMember]
        public string CreatorNameText { get; set; }

        [DataMember]
        public string Creator { get; set; }

        [DataMember]
        public DateTime CreationDate { get; set; }
    }

    public partial class Music : BaseData
    {
        public override void BeforeSending()
        {
            Author = Commentable.Creator.Trim();
            AuthorName = Commentable.User.GetName();
            //Author = Author.Trim();
            AuthorLogin =  Commentable.Creator.Trim();
            FileFormat = (MusicFileFormat) Format;

            double? rate = null;
            int count = 0;

            if (RateDance != null)
            {
                rate = RateDance.Value;
                count++;
            }

            if(RateMusic!=null)
            {
                rate = rate.GetValueOrDefault() + RateMusic.Value;
                count++;
            }

            if (RateQuality != null)
            {
                rate = rate.GetValueOrDefault() + RateQuality.Value;
                count++;
            }

            if(rate!=null)
            AverageRate = rate/count;

            Name = Commentable.Name;
        }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Author { get; set; }

        [DataMember]
        public double? AverageRate { get; set; }


        public void PrepareBigSending(byte[] noPhotoImage)
        {
            BeforeSending();
            if (Image != null) ImageBs = Image.ToArray();
            else
            {
                if(Commentable.User.Image!=null)
                {
                    ImageBs = Commentable.User.Image.ToArray();
                }
                else
                {
                    ImageBs = noPhotoImage;
                }
            }
        }

        [DataMember]
        public string AuthorName { get; set; }

        [DataMember]
        public string AuthorLogin { get; set; }

        [DataMember]
        public byte[] ImageBs { get; set; }

        [DataMember]
        public MusicFileFormat FileFormat { get; set; }
    }
}
